package com.yswl.yeeduo.ui.tool;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.yswl.yeeduo.tool.KStringTool;

//做单例
public class UserManger {

	private static UserManger userManger;

	private UserManger() {
		super();
	}

	public static UserManger getInstance() {
		if (userManger == null) {
			userManger = new UserManger();
		}
		return userManger;
	}

	private static final String USER_NAME = "user_name";
	private static final String USER_PWD = "user_pwd";
	
	private static final String USER_STATE = "user_state";
	private static final String WX_STATE = "wx_state";
	private static final String WX_ACCESS_TOKEN = "access_token";
	private static final String WX_UNIONID = "unionid";
	private static final String WX_OPENID = "openid";

	/** 用户是否微信登录授权 */
	public boolean isAuthed(Context context) {
		List<String> params = getWxLoginParam(context);
		if (params == null || params.size() <= 0) {
			return false;
		}
		for (Iterator<String> iter = params.iterator(); iter.hasNext();) {
			if (KStringTool.isEmpty((String) iter.next())) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 记录微信授权成功后的参数 String access_token = value.getString("access_token");
	 * String unionid = value.getString("unionid"); String openid =
	 * value.getString("openid");
	 * 
	 * @param context
	 */
	public void recordWxParameter(Context context, String access_token, String unionid, String openid) {
		SharedPreferences sp = context.getSharedPreferences(WX_STATE, Activity.MODE_PRIVATE);
		Editor edit = sp.edit();
		edit.putString(WX_ACCESS_TOKEN, access_token);
		edit.putString(WX_UNIONID, unionid);
		edit.putString(WX_OPENID, openid);
		edit.commit();
	}
	/**
	 * 此方法专门留给 用户退出使用 传入null 或者“” 即可
	 * @param context
	 * @param openid
	 */
	public void recordWxParameterOfOpenId(Context context,  String openid) {
		SharedPreferences sp = context.getSharedPreferences(WX_STATE, Activity.MODE_PRIVATE);
		Editor edit = sp.edit();
		edit.putString(WX_OPENID, openid);
		edit.commit();
	}

	public List<String> getWxLoginParam(Context context) {
		SharedPreferences sp = context.getSharedPreferences(WX_STATE, Activity.MODE_PRIVATE);
		List<String> list = new ArrayList<String>();
		list.add(sp.getString(WX_ACCESS_TOKEN, ""));
		list.add(sp.getString(WX_UNIONID, ""));
		list.add(sp.getString(WX_OPENID, ""));
		return list;
	}
	
	
	public void recordUserName(Context context, String userName) {
		SharedPreferences sp = context.getSharedPreferences(USER_STATE, Activity.MODE_PRIVATE);
		Editor edit = sp.edit();
		edit.putString(USER_NAME, userName);
		edit.commit();
	}
//	public void recordUserPwd(Context context, String userPwd) {
//		SharedPreferences sp = context.getSharedPreferences(USER_STATE, Activity.MODE_PRIVATE);
//		Editor edit = sp.edit();
//		edit.putString(USER_PWD, userPwd);
//		edit.commit();
//	}
//	public void recordUserInfo(Context context, String userName,String userPwd) {
//		SharedPreferences sp = context.getSharedPreferences(USER_STATE, Activity.MODE_PRIVATE);
//		Editor edit = sp.edit();
//		edit.putString(USER_NAME, userName);
//		edit.putString(USER_PWD, userPwd);
//		edit.commit();
//	}
	public String getUserName(Context context) {
		SharedPreferences sp = context.getSharedPreferences(USER_STATE, Activity.MODE_PRIVATE);
		return sp.getString(USER_NAME, "");
	}
//	public String getUserPwd(Context context) {
//		SharedPreferences sp = context.getSharedPreferences(USER_STATE, Activity.MODE_PRIVATE);
//		return sp.getString(USER_PWD, "");
//	}
	
	
}
